
$(function () {
  var mySwiper = new Swiper('.swiper', {
    // direction: 'vertical', // 垂直切换选项
    loop: true, // 循环模式选项
    // autoplay:true,
    // 如果需要分页器
    pagination: {
      el: '.swiper-pagination',
    },

    // 如果需要前进后退按钮
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },

    // 如果需要滚动条
    // scrollbar: {
    //   el: '.swiper-scrollbar',
    // },
  })

  //   获取图片数据

  $.ajax({
    method: 'get',
    url: 'http://124.223.14.236:8060/index/index',
    success: function ({ data }) {
      console.log(data);
      console.log(data.banner);
      // console.log('获取成功');
      let newPic = data.banner.map((item) => {
        let {advimgdesc,advimglink,advimgsrc } = item
        return `
        <div class="swiper-slide">
       <img src="http://124.223.14.236:8060/${advimgsrc}" alt="${advimglink}"> 
      </div>
                `
      }).join('')

      $('.swiper-wrapper').html(newPic)
    }
  })


  // 热门推荐
  $.ajax({
    method: 'get',
    url: 'http://124.223.14.236:8060/index/hot',
    success: function ({ data }) {
      // console.log(data);
      // console.log(data.list);

      let newRecoment = data.list.map((item) => {
        return `
      <li class="item">
      <a target="_blank" href="">
      <img src="http://124.223.14.236:8060/${item.pic}"class="thumb" />${item.title}</a>
      </li>
      `
      }
      ).join('')
      //    console.log(newRecoment);
      $('#fire').html(newRecoment)
    }
  })

  //时间戳转换年月日
  function paddZero(n) { //补零函数
    return n > 10 ? n : '0' + n
  }

  function formatTime(time) {
    let d = new Date(time)

    let y = d.getFullYear()
    let month = paddZero(d.getMonth() + 1)
    let day = paddZero(d.getDate())


    return `${y}/${month}/${day}`
  }


  //   右侧广告位
  $.ajax({
    method: 'get',
    url: 'http://124.223.14.236:8060/index/index',
    success: function ({ data }) {
      // console.log(data);
      // console.log(data.sideBanner);

      let rightList = data.sideBanner.map((item) => {
        let { advimgdesc, advimglink, advimgsrc } = item
        return `
            <div class="textwidget">
            <a href="${advimglink}" rel="nofollow" target="_blank" title="阿里云开学季">
              <img class="g430" style="width:360px;height:133px;" src="http://124.223.14.236:8060/${advimgsrc}" alt="${advimgdesc}" >
            </a>
           
          </div>
            `
      }).join('')
      $(".textwidget").html(rightList)


      //右侧 有情链接
      let newLianjie = data.allLink.map((item) => {
        // console.log(item);

        let { title, url, img, des } = item
        // if(type === 1){
        return `
              <li>
              <a href="${url}">
                <img src="http://124.223.14.236:8060/${img}" alt="">
                <p>${des}</p>
              </a>
            </li>
              `
      }).join('')
      $('#widget').html(newLianjie)

      //  底部有情链接
      let footLianjie = data.allLink.map((item) => {
        // console.log(item);
        let { title, url, img, des } = item
        return `
      <a href="" title="${url}">${des}</a> <span>|</span>
      `
      }).join('')
      $('.footer .link').html(footLianjie)

      // 热门标签
      // console.log(data.allTag);
      // console.log(data);
      let newBiaoqian = data.allTag.map((item) => {
        return `
            <li><a title="" href="">${item.tagname}</a></li>
            `
      }).join('')
      $('.widget_tags_inner').html(newBiaoqian)

    }
})


  // 热门文章
  $.ajax({
    method: 'get',
    url: 'http://124.223.14.236:8060/home/index/recommend',
    success: function ({ data }) {
      // console.log(data);
      // console.log(data.list);
      let newrec = data.list.map((item) => {
        let { pic, title, lovenum } = item

        return `
            <li>
            <a target="_blank" href="">
              <span class="thumbnail">
                <img src="http://124.223.14.236:8060/${pic}" class="thumb" />
              </span>
              <span class="text">${title}</span>
              <span class="text-muted post-views">${lovenum}</span>
            </a>
          </li>
            `
      }).join('')
      $('.widget .items-02').html(newrec)
    }
  })






  layui.use('laypage', function () {
    // 开始写页面渲染
    let query = {
      cateid: 4,
      page: 1,
      limit: 10,
    }
    let getart = function () {
      // 最新更新
      $.ajax({
        method: 'get',
        url: 'http://124.223.14.236:8060/index/list',

        data: query,
        success: function ({ data: { list } }) {
          // console.log(data);
          
          console.log(list.count)
          renderLayPage(list.count)
          console.log(list.data);
          // console.log(data.list);
          let newList = list.data.map((item) => {

            // console.log(item);
            let { author, catename, title, keywords, pic, tags, istop, lovenum, description, create_date, click, id } = item
            return `
        <article class="excerpt excerpt-one">
              <header>
                <a class="cat label label-important" href="">享·优惠<i class="label-arrow"></i></a>
                <h2>
                  <a target="_blank" id='indexid' href="show.html"  title="${title}" data-id="${id}"  id="show">${title}/三年279元</a>
                </h2>
              </header>
              <p class="text-muted time">
                <a href="">${author}</a> 发布于 ${formatTime(create_date)}
              </p>
              <p class="focus">
                <a target="_blank" href="http://blog.weknow.cn/474.shtml" class="thumbnail">
                  <span  class="item"><span class="thumb-span"><img  src="http://124.223.14.236:8060/${pic}"  class="thumb" /></span></span></a>
                </p>
              <p class="note">${description}</p>
              <p class="text-muted views">
                <span class="post-views">阅读(${click})</span>
                <span class="post-comments">评论(0)</span>
                <a href="javascript:;" class="post-like">
                  <i class="fa fa-thumbs-up"></i>赞 (<span>${lovenum}</span>)
                </a>
                <span class="post-tags">标签：
                  <a href=""  rel="tag">${tags}</a> / 
                  <a href=""  rel="tag">${catename}</a></span>
               </p>
            </article>
        `
          }).join('')
          $('#indexfl').html(newList)

        }

      })

    }
    getart()



    // 分页
    function renderLayPage(count) {
      const laypage = layui.laypage
      laypage.render({
        elem: 'page', // 注意，这里的 page-box 是 ID，不用加 # 号
        count, //从query对象中可以获取到
        curr: query.page, // 每页条数，从query对象中可以获取到
        limit: query.limit, //起始值
        limits: [10, 20, 40, 50, 100],
        layout: ['count', 'limit', 'prev', 'page', 'next', 'skip'],
        jump: function (obj, first) {
          // console.log(obj);
          query.page = obj.curr
          query.limit = obj.limit
          if (!first) {
            getart()
          }
        }
      })
    }
  })
})

$('article').on('click', '#indexid', function () {
  const id = $(this).attr('data-id')
  localStorage.setItem('id', id)
})




